package com.zsguan.z.meixi;

import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ProgressBar;

import com.zsguan.s.stools4andr.base.BaseActivity;
import com.zsguan.z.slogger4andr.ZLogger;
import com.zsguan.z.zdownload4andr.DownloadReceiver;
import com.zsguan.z.zdownload4andr.DownloadService;
import com.zsguan.z.zdownload4andr.Downloads;

import java.io.File;

/**
 * Created by chshqiang on 15-9-4.
 * http和https请求
 */
public class ZDownloadActivity extends BaseActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String WHERE_SUCCESS = "(" + Downloads.COLUMN_VID
            + " = '1') AND (" + Downloads.COLUMN_VISIBILITY + " IS NULL OR "
            + Downloads.COLUMN_VISIBILITY + " == '"
            + Downloads.VISIBILITY_VISIBLE + "' OR "
            + Downloads.COLUMN_VISIBILITY + " == '"
            + Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED + "')";

    private ProgressBar download_progress;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        download_progress = (ProgressBar) findViewById(R.id.download_progress);

        setup();
    }

    private void setup() {
        ContentValues values = new ContentValues();
        values.put(Downloads.COLUMN_VID, 1);
        getContentResolver().delete(Downloads.CONTENT_URI, "(vid='1')", null);

        startService(new Intent(ZDownloadActivity.this, DownloadService.class));
        getLoaderManager().restartLoader(0, null, ZDownloadActivity.this);
    }

    private void update(Cursor cursor) {
        if (null == cursor || 0 >= cursor.getCount() || !cursor.moveToFirst()) {
            return;
        }

        final int id = cursor.getInt(cursor.getColumnIndex(Downloads._ID));
        final String path = cursor.getString(cursor
                .getColumnIndex(Downloads.COLUMN_FILE_NAME_HINT));
        final String downloadurl = cursor.getString(cursor
                .getColumnIndex(Downloads.COLUMN_URI));
        String url = cursor.getString(cursor
                .getColumnIndex(Downloads.COLUMN_PIC_PATH));
        int control = cursor.getInt(cursor
                .getColumnIndex(Downloads.COLUMN_CONTROL));
        int total = cursor.getInt(cursor
                .getColumnIndex(Downloads.COLUMN_TOTAL_BYTES));
        int pgs = cursor.getInt(cursor
                .getColumnIndex(Downloads.COLUMN_CURRENT_BYTES));
        String nameTmp = cursor.getString(cursor
                .getColumnIndex(Downloads.COLUMN_TITLE));

        download_progress.setMax(total);
        download_progress.setProgress(pgs);

        ZLogger.i("update: " + id + ", " + downloadurl + ", " + path + ", " + url + ", " + control + ", " + total + ", " + pgs + ", " + nameTmp);
    }

    public void onDownload(View view) {
        int vid = 1;
        String uri = "http://staging2.mowenhh.com:51080/maps/15/20150907182711/20150907182711jQLh9eVI.zip";
        String name = "20150907182711jQLh9eVI";
        String picPath = "http://staging2.mowenhh.com:51080/maps/15/20150907182711/20150907182711xBS1exmQ.jpg";

        ContentValues values = new ContentValues();
        values.put(Downloads.COLUMN_VID, vid);
        values.put(Downloads.COLUMN_URI, uri);
        values.put(Downloads.COLUMN_PIC_PATH, picPath);
        values.put(Downloads.COLUMN_MIME_TYPE, "*/*");
        values.put(Downloads.COLUMN_TITLE, name);
        values.put(Downloads.COLUMN_FILE_NAME_HINT, name + ".zip");
        values.put(Downloads._DATA, Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "downloads" + File.separator +  name + ".zip");
        values.put(Downloads.COLUMN_VISIBILITY, Downloads.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        values.put(Downloads.COLUMN_NOTIFICATION_CLASS, DownloadReceiver.class.getName());
        values.put(Downloads.COLUMN_NOTIFICATION_PACKAGE, getPackageName());
        values.put(Downloads.COLUMN_NO_INTEGRITY, true);
        getContentResolver().insert(Downloads.CONTENT_URI, values);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLoaderManager().destroyLoader(0);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        CursorLoader cursorLoader = new CursorLoader(ZDownloadActivity.this,
                Downloads.CONTENT_URI, new String[]{Downloads._ID,
                Downloads.COLUMN_TITLE, Downloads.COLUMN_URI, Downloads.COLUMN_CONTROL,
                Downloads.COLUMN_TOTAL_BYTES,
                Downloads.COLUMN_CURRENT_BYTES,
                Downloads.COLUMN_FILE_NAME_HINT, Downloads.COLUMN_VID,
                Downloads.COLUMN_PIC_PATH, Downloads.COLUMN_STATUS},
                WHERE_SUCCESS, null, Downloads._ID);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        update(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}
